<?php

namespace App\Http\Controllers\business;

use App\Models\Dictionary;
use App\Models\Product;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

class ProductController extends Controller
{
    protected $pageSize=10;


    /**产品库列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function index(Request $request){
        return redirect("/wap/member");
        if(!Session::get('manager.id')){
            if(Session::get('manager.perfix') == 'wx')
            {
                return redirect(url('wx/register'));
            }else{
                return redirect(url('wap/login'));
            }
        }
        if($request->isMethod('POST')){
            $category_id=$request->input('category_id','');
            if($category_id==''){
                return response()->json(['status'=>201,'msg'=>'分类id不能为空']);
            }
            $products=Product::select('id','name','apply_num','tag_ids','basic_info')
                ->where('is_hot','!=',1)->where(DB::raw('convert(basic_info->"$.category_id_first",signed)'),$category_id)->paginate($this->pageSize);
            $products->appends(['category_id'=>$category_id]);
            return response()->json(['status'=>200,'msg'=>'ok','data'=>$products]);
        }else{
            $categories=Dictionary::select('id','name')->where(['pid'=>1])->get();
            if(!$categories->count()){
                abort(404,'贷款类型有误');
            }
            foreach ($categories as $category){
                $category->hots=Product::select('id','name','basic_info')
                    ->where(['is_hot'=>1,'status'=>1])
                    ->where(DB::raw('convert(basic_info->"$.category_id_first",signed)'),$category->id)
                    ->orderBy('apply_num','desc')->orderBy('created_at','desc')->get();
                $category->products=Product::select('id','name','apply_num','tag_ids','basic_info')
                    ->where('is_hot','!=',1)->where(['status'=>1])
                    ->where(DB::raw('convert(basic_info->"$.category_id_first",signed)'),$category->id)
                    ->orderBy('apply_num','desc')
                    ->orderBy('id','desc')
                    ->paginate($this->pageSize);
                $category->products->appends(['category_id'=>$category->id]);
            }
            $pageSize=$this->pageSize;
            return view('business.product.index',compact(['categories','pageSize']));
        }

    }

    /**
     * 产品搜索
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function search(Request $request){

        $keyword=$request->input('keyword','');
        $products=null;
        if($keyword){
            $products=Product::select('id','name','apply_num','tag_ids','basic_info')
                ->where(['status'=>1])->where('name','like',"%$keyword%")
                ->orderBy('apply_num','desc')->orderBy('created_at','desc')->paginate();
            $products->appends(['keyword'=>$keyword]);
            $products->keyword=$keyword;
            if($request->isMethod('POST')){
                return response()->json(['status'=>200,'msg'=>'ok','data'=>$products]);
            }
        }
        return view('business.product.search',compact(['products']));
    }

    /**产品详情
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show($id){

        $product=Product::findOrFail($id);
        return view('business.product.show',compact('product'));

    }


}
